<?php
/*

Copyright 2009 Matthew C. Dettinger

This program is free software: you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

function dao_data_group_add_new_data_group($config, $data_group_name) {
	$link = c_establish_db_conn($config);
	
	//check to see that the name handed is in valid form
	if(!$regex_status = eregi("^[a-z0-9]{1,32}$", $data_group_name)) {
		$msg = "Group name must contain blah!";
		c_announce_user_error($msg);
	}
	
	//check to see if group name is already in database
	$query = "SELECT * FROM data_group WHERE name = '$data_group_name' AND user_uuid = '$_SESSION[user_uuid]'";
	$result = mysqli_query($link, $query);
	if(mysqli_num_rows($result) != 0) {
		// this is already in the db
		$msg = "You already have a group by that name.";
		c_announce_user_error($msg);
	}
	
	// generate uuid for new entry
	$uuid = c_generate_uuid();
	$query = "INSERT INTO data_group VALUES('$uuid', '$_SESSION[user_uuid]', '$data_group_name')";
	mysqli_query($link, $query);

	return $uuid;
}

function dao_data_group_del_data_group($company_uuid) {
	
}

function dao_data_group_list_user_data_groups($config, $html = '0') {
	$link = c_establish_db_conn($config);
	$query = "SELECT * FROM data_group WHERE user_uuid = '$_SESSION[user_uuid]'";
	$result = mysqli_query($link, $query);
	$group_list = array();
	while ($row = mysqli_fetch_assoc($result)) {
		$data_group_list[$row[uuid]] = $row[name];
	}
	return $data_group_list; // array
}


?>